var HOST = $('body').attr('data-host');  // 请求 HOST 地址

$(function () {

    $('form').on('click', "button[type='submit']", function () {
        var account = $('form').find("input[type='text']").val();
        var password = $('form').find("input[type='password']").val();
        if (account && password) {
            $('form').find("button[type='submit']").find('i').removeClass('hide');
            $('form').find("button[type='submit']").attr('disabled', 'disabled').text('正在登陆中...请稍后');
            $.ajax({
                type: 'post',
                url: HOST + '/login/in',
                data: {
                    account: account,
                    password: password
                },
                dataType: "json",
                success: function (e) {
                    if (e.state) {
                        // toastr.success('正在跳转，请等待...', '登录成功');
                        $('form').find("button[type='submit']").text('登录成功，正在前往后台');
                        setTimeout(function () {
                            if ($.cookie('LastPage')) {
                                window.location.href = $.cookie('LastPage');
                            }
                            window.location.href = HOST + '/index';
                        }, 1000);
                    } else if (!e.state) {
                        toastr.warning('好像账号或密码不正确噢', '登录失败');
                        $('form').find("button[type='submit']").removeAttr('disabled').text('安全登录');
                    }
                    $('form').find("button[type='submit']").find('i').addClass('hide');
                }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log('server error:' + XMLHttpRequest.status); // 错误代码
                    toastr.error('请联系程序猿修复:[' + XMLHttpRequest.status + ']', '噢天、服务器挂了');
                }
            });
        }
    });

});